package com.cloudxingkong.dianyinke.Student.Ui;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.IsMobile;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.LoginJsonUtils;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;

/**
 * 添加地点
 */
public class AddSite extends AppCompatActivity {
    private EditText editText_name, editText_phoneNum, editText_site, editText_mailbox;
    private String name, phones, site, mailbox;
    private Button btn_conf;
    private CheckBox checkBos_site;
    private SharedPreferences sharedPreferences;
    private ImageButton back_back;
    private ProgressDialog progressDialog;
    private boolean flag = false;
    private int index=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_site);
        init();
    }

    private void init() {
        editText_name = (EditText) findViewById(R.id.editText_name);
        editText_phoneNum = (EditText) findViewById(R.id.editText_phoneNum);
        editText_site = (EditText) findViewById(R.id.editText_site);
        editText_mailbox = (EditText) findViewById(R.id.editText_mailbox);

        btn_conf = ((Button) findViewById(R.id.btn_conf));
        checkBos_site = (CheckBox) findViewById(R.id.checkBos_site);
        back_back = ((ImageButton) findViewById(R.id.back_back));
        sharedPreferences = getSharedPreferences("data_dyk", 0);
    }


    public void back(View view) {
        switch (view.getId()) {
            case R.id.back_back:
                finish();
                break;
            case R.id.btn_conf:

                if (TextUtils.isEmpty(editText_name.getText()) ||
                        TextUtils.isEmpty(editText_phoneNum.getText()) ||
                        TextUtils.isEmpty(editText_site.getText()) ||
                        TextUtils.isEmpty(editText_mailbox.getText())) {
                    Toast.makeText(this, "内容不完整...", Toast.LENGTH_SHORT).show();
                } else {
                    boolean mobileNO = IsMobile.isMobileNO(editText_phoneNum.getText().toString());
                    if (!mobileNO) {
                        Toast.makeText(AddSite.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    progressDialog = ProgressDialog.show(this, null, "正在为您添加...", true, false);

                    name = editText_name.getText().toString();
                    phones = editText_phoneNum.getText().toString();
                    site = editText_site.getText().toString();
                    mailbox = editText_mailbox.getText().toString();

                    boolean checked= checkBos_site.isChecked();
                    if (checked==true){
                        index=1;
                    }else {
                        index=0;
                    }

                    final String sessionid = CookieHelper.getSessionID(sharedPreferences);
                    //区域名+接口地址
                    final String upLoadUrl = ThisUri.SERVER_YUMING+ StudentUri.ADDSITE;

                    RequestParams params = new RequestParams(upLoadUrl);
                    params.setHeader("cookie", sessionid);
                    params.setMultipart(true);
                    params.addBodyParameter("people", name);//姓名
                    params.addBodyParameter("phone", phones);//电话号码
                    params.addBodyParameter("addr", site);//地址
                    params.addBodyParameter("poscode", mailbox);//邮箱
                    params.addBodyParameter("isdefault",index+"");//checked 状态

                    x.http().post(params, new Callback.CommonCallback<String>() {

                        @Override
                        public void onSuccess(String result) {
                            Log.e("test","上传结果"+result);
                            if (result!=null&&!result.equals("")){
                                Message message = handler.obtainMessage();
                                message.what = 1;
                                message.obj = result;
                                handler.sendMessage(message);
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {
                        }
                    });
                }
                break;
        }
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                String state = (String) msg.obj;
                Log.e("test","上传结果 "+state);
                if (state!=null&&!state.equals("")){
                    HashMap<String, Object> jsonFromLogin = LoginJsonUtils.getJsonFromLogin(state);
                    flag = (boolean) jsonFromLogin.get("success");
                    if (flag) {
                        Toast.makeText(AddSite.this, "上传成功！", Toast.LENGTH_SHORT).show();
                        setResult(666);
                        progressDialog.dismiss();
                        finish();
                    } else {
                        Toast.makeText(AddSite.this, "上传失败！", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                        finish();
                    }
                }
            }
        }
    };
}
